package com.talktalk.utils;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

public class MyQueryRunner extends QueryRunner{

	@Override
	public int[] batch(String sql, Object[][] params) throws SQLException {
		Connection conn = JdbcUtils.getConnection();
		int[] batch = super.batch(conn,sql, params);
		JdbcUtils.closeConnection(conn, null, null);
		return batch;
	}

	@Override
	public <T> T query(String sql, Object param, ResultSetHandler<T> rsh)
			throws SQLException {
		Connection conn = JdbcUtils.getConnection();
		T query = super.query(conn,sql, param, rsh);
		JdbcUtils.closeConnection(conn, null, null);
		return query;
	}

	@Override
	public <T> T query(String sql, Object[] params, ResultSetHandler<T> rsh)
			throws SQLException {
		Connection conn = JdbcUtils.getConnection();
		T query = super.query(conn,sql, params, rsh);
		JdbcUtils.closeConnection(conn, null, null);
		return query;
	}

	@Override
	public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params)
			throws SQLException {
		Connection conn = JdbcUtils.getConnection();
		T query = super.query(conn,sql, rsh, params);
		JdbcUtils.closeConnection(conn, null, null);
		return query;
	}

	@Override
	public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException {
		Connection conn = JdbcUtils.getConnection();
		T query = super.query(conn,sql, rsh);
		JdbcUtils.closeConnection(conn, null, null);
		return query;
	}

	@Override
	public int update(String sql) throws SQLException {
		Connection conn = JdbcUtils.getConnection();
		int update = super.update(sql);
		JdbcUtils.closeConnection(conn, null, null);
		return update;
	}

	@Override
	public int update(String sql, Object param) throws SQLException {
		Connection conn = JdbcUtils.getConnection();
		int update = super.update(sql,param);
		JdbcUtils.closeConnection(conn, null, null);
		return update;
	}

	@Override
	public int update(String sql, Object... params) throws SQLException {
		Connection conn = JdbcUtils.getConnection();
		int update = super.update(conn,sql, params);
		JdbcUtils.closeConnection(conn, null, null);
		return update;
	}
	
	
	
}
